package com.e3.basdoc.goods.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.basdoc.goods.dao.GoodsDAO;
import com.e3.basdoc.goods.entity.GoodsEntity;
import com.e3.basdoc.goods.model.GoodsModel;
import com.e3.basdoc.goods.model.GoodsQC;
import com.e3.basdoc.goods.service.GoodsCacheManager;
import com.e3.basdoc.goods.service.GoodsQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class GoodsQueryServiceImpl extends SuperQueryService implements GoodsQueryService{
	
	private GoodsDAO goodsDAO;
	
	private GoodsDAO getGoodsDAO(){
		if(goodsDAO == null){
			goodsDAO = DAOHelper.createDAOInstance(GoodsDAO.class);
		}
		return goodsDAO;
	}

	public List<GoodsModel> getGoodsByCondition(GoodsQC qc) throws ServiceException {
		
		List<Long> ids = getGoodsDAO().getGoodsIdsByCondition(qc);
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<GoodsModel> models = new ArrayList<GoodsModel>(ids.size());
		for (Long id : ids) {
			GoodsModel model = getGoodsById(id);
			models.add(model);
		}
		return models;
	}
	/**
	 * the method is used to load data into cache.
	 */
	public List<GoodsModel> getAllGoodss() throws ServiceException {
		List<GoodsEntity> entities = getGoodsDAO().getAllGoods();
		if(entities==null||entities.isEmpty()){
			return null;
		}
		List<GoodsModel> models = new ArrayList<GoodsModel>(entities.size());
		for (GoodsEntity goodsEntity : entities) {
			models.add(goodsEntityToModel(goodsEntity));
		}
		return models;
	}

	public GoodsModel getGoodsById(long id) throws ServiceException {
		GoodsModel model = GoodsCacheManager.INSTANCE.get(id);
		if(model==null){
			GoodsEntity entity = getGoodsDAO().getById(GoodsEntity.class, id);
			model = goodsEntityToModel(entity);
			if(model != null){
				GoodsCacheManager.INSTANCE.put(model);
			}
		}
		
		return model;
	}

	public List<GoodsModel> getGoodsByIds(List<Long> ids)	throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<GoodsModel> models = new ArrayList<GoodsModel>(ids.size());
		for (Long id : ids) {
			GoodsModel model = getGoodsById(id);
			models.add(model);
		}
		
		return models;
	}

	private GoodsModel goodsEntityToModel(GoodsEntity entity){
		
		if(entity == null){
			return null;
		}
		
		GoodsModel model = new GoodsModel();
		model = ocEntityToModel(model, entity);
		
		model.setCode(entity.getCode());
		model.setName(entity.getName());
		model.setShortName(entity.getShortName());
		model.setAllowNegative(entity.getAllowNegative());
		model.setAllowReturn(entity.getAllowReturn());
		model.setBarcode(entity.getBarcode());
		model.setBrand(entity.getBrand());
		model.setBulk(entity.getBulk());
		model.setCeilingPrice(entity.getCeilingPrice());
		model.setExportRebatesRate(entity.getExportRebatesRate());
		model.setFloorPrice(entity.getFloorPrice());
		model.setForeignLangName(entity.getForeignLangName());
		model.setHasInvoice(entity.getHasInvoice());
		model.setOrigin(entity.getOrigin());
		model.setPurchaseLossRate(entity.getPurchaseLossRate());
		model.setReserveLossRate(entity.getReserveLossRate());
		model.setRefCost(entity.getRefCost());
		model.setRefPrice(entity.getRefPrice());
		model.setRemark(entity.getRemark());
		model.setSealFlag(entity.getSealFlag());
		model.setSealPersonId(entity.getSealPersonId());
		
		Long time = entity.getSealTime();
		if(time!=null&&time > 0){
			model.setSealTime(new Date(entity.getSealTime()));
		}
		
		model.setTaxId(entity.getTaxId());
		model.setType(entity.getType());
		model.setMainSupplierId(entity.getMainSupplierId());
		model.setMeasureUnitId(entity.getMeasureUnitId());
		model.setSpecification(entity.getSpecification());
		model.setWeight(entity.getWeight());
		
		return model;
	}

	public List<GoodsModel> getPagingGoods(GoodsQC qc, int start, int max)	throws ServiceException {
		List<Long> ids = getGoodsDAO().getPagingGoodsIdsByCondition(qc,start,max);
		if(ids!=null&&ids.size()>0){
			return getGoodsByIds(ids);
		}
		return null;
	}

	public Long getGoodsSizeByCondition(GoodsQC qc) throws ServiceException {
		return getGoodsDAO().counterByCondition(qc);
	}
}
